package com.aaron.kjviewutils;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：Aaron on 2017/2/14 17:32
 * 邮箱：kaijin.fang@edianniu.com
 */
public class MobDetailActivity extends BaseActivity {

    @Bind(R.id.webToolBar)
    Toolbar webToolBar;
    @Bind(R.id.leftBarIcon)
    ImageView leftBarIcon;
    @Bind(R.id.barTitle)
    TextView barTitle;
    @Bind(R.id.webSwip)
    SwipeRefreshLayout webSwip;
    @Bind(R.id.webI)
    WebView webI;

    private String title;
    private String webUrl;

    @OnClick(R.id.leftBarIcon)
    void leftBarIconClick()
    {
        /*if(null!=menu)
        {
            menu.showMenu(true);
        }*/
        closeSelf(MobDetailActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.layout_mob);
        ButterKnife.bind(this);
        title = this.getIntent().getExtras().getString("title");
        webUrl = this.getIntent().getExtras().getString("url");
        initTitle();
        initWeb();
    }

    private void initWeb()
    {
        mHandler.postDelayed(runnableLoadWeb, 500);
    }

    Runnable runnableLoadWeb = new Runnable() {
        @Override
        public void run() {
            webSwip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    //重新刷新页面
                    webI.loadUrl(webI.getUrl());
                }
            });
            webSwip.setColorScheme(android.R.color.holo_blue_bright,
                    android.R.color.holo_green_light, android.R.color.holo_orange_light,
                    android.R.color.holo_red_light);
            webI.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
            webI.setVerticalScrollBarEnabled(false);
            //是否需要避免页面放大缩小操作
            webI.getSettings().setBuiltInZoomControls(true);
            webI.getSettings().setJavaScriptEnabled(true);
            webI.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            webI.getSettings().setUseWideViewPort(true);
            webI.getSettings().setLoadWithOverviewMode(true);
            webI.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            webI.getSettings().setDisplayZoomControls(false);
            webI.getSettings().setAllowFileAccess(true); // 允许访问文件
            webI.getSettings().setSupportZoom(true); // 支持缩放
            webI.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
            webI.getSettings().setDomStorageEnabled(true);
            //android4.4之前设置图片延后加载
            if(19<= getSDKVersion())
            {
                webI.getSettings().setLoadsImagesAutomatically(true);
            }
            else
            {
                webI.getSettings().setLoadsImagesAutomatically(false);
            }
            webI.loadUrl(webUrl);
            if(null!=webI)
            {
                webI.setWebViewClient(new WebViewClient() {

                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        return super.shouldOverrideUrlLoading(view, url);
                    }

                    @Override
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);
                        if(!webI.getSettings().getLoadsImagesAutomatically()) {
                            webI.getSettings().setLoadsImagesAutomatically(true);
                        }
                    }
                });
                webI.setWebChromeClient(new WebChromeClient()
                {
                    @Override
                    public void onReceivedTitle(WebView view, String title) {
                        super.onReceivedTitle(view, title);
                    }

                    @Override
                    public void onProgressChanged(WebView view, int newProgress) {
                        if(null!=webSwip)
                        {
                            if (newProgress == 100) {
                                //隐藏进度条
                                webSwip.setRefreshing(false);
                            } else {
                                if (!webSwip.isRefreshing())
                                    webSwip.setRefreshing(true);
                            }
                        }
                        super.onProgressChanged(view, newProgress);
                    }


                });
            }
        }
    };

    private void initTitle()
    {
        webToolBar.setTitle("");
        barTitle.setText((TextUtils.isEmpty(title)?"未知":title));
        loadBackground(leftBarIcon, resources.getDrawable(R.mipmap.back));
        webToolBar.setNavigationIcon(null);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            //关闭
            closeSelf(MobDetailActivity.this);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null!=mHandler)
        {
            mHandler.removeCallbacks(runnableLoadWeb);
        }
        if(null!=webI)
        {
            webI.destroy();
        }
        ButterKnife.unbind(this);
    }
}
